package top.tangyh.lamp.common.utils;
import com.alibaba.excel.write.handler.RowWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;

public class CustomRowWriteHandler implements RowWriteHandler {
    /**
     * 实际数据列号
     */
    private int count = 0;

    private CellStyle firstCellStyle;

    @Override
    public void beforeRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Integer rowIndex, Integer relativeRowIndex, Boolean isHead) {

    }

    @Override
    public void afterRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer relativeRowIndex, Boolean isHead) {
        Cell cell = row.createCell(0);
        firstCellStyle = row.getRowStyle();
        // 标题样式单独设置
        if (row.getRowNum() == 0) {
            Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
            firstCellStyle = CellStyleUtil.firstTitleCellStyle(workbook);
            cell.setCellStyle(firstCellStyle);
            cell.setCellValue("序号");
            return;
        }
        // 第一行先清除标题样式
        if(row.getRowNum() == 1){
            firstCellStyle = null;
        }
        // 设置内容样式
        if (firstCellStyle == null) {
            Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
            firstCellStyle = CellStyleUtil.firstCellStyle(workbook);
        }
        cell.setCellStyle(firstCellStyle);
        cell.setCellValue(++count);
    }

    @Override
    public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer relativeRowIndex, Boolean isHead) {

    }
}
